package com._21cn.fetch.utils;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.utils.FormatContentUtil;
import com._21cn.framework.http.HttpCaller;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;

public class Fetch60mil {
    private static String postUrl = "http://admin.cms.qiaoliba.com/admin/article/article/save.do";

    private String linkPattern = "\\d+_\\d+.shtml";
    private Set<String> linkSet = new TreeSet();

    public Document getDocument(String url) {
        HttpCaller httpCaller = HttpCaller.get(url);
        String html = httpCaller.call().getString();
        Document doc = Jsoup.parse(html);
        return doc;
    }

    public Set<String> getPager(Document document) {
        Elements lastPage = document.select(".lastPage");
        Elements articleIdEle = document.select(".articleId");
        Integer pageNo = Integer.valueOf(lastPage.get(0).text());;
        String articleId = articleIdEle.get(0).text();
        for(int i=1; i<=pageNo; i++){
            linkSet.add(articleId+"_"+i+".shtml");
        }
        return linkSet;
    }

    public Article getArticle(String contentUrl) {
        Document doc = getDocument(contentUrl);
        Article article = formatArticle(doc);
        Set<String> pager = getPager(doc);
        int i = 1;
        for (String url : pager) {
            int index = contentUrl.lastIndexOf("/");
            String before = contentUrl.substring(0, index + 1);
            url = before + "/" + url;
            Document document = getDocument(url);
            String content = FormatContentUtil.formatContent(document.select(".content").html()).replaceAll("<div>", "").replaceAll("</div>", "");
            if (!StringUtil.isEmpty(content)) {
                if( i!=1 ){
                    content = article.getContent() + "<p class=\"pager\">&nbsp;</p>" + content;
                    article.setContent(content);
                }
            }
            i++;
        }
        return article;
    }

    public Article formatArticle(Document doc) {
        Article article = new Article();
        String title = doc.select(".title").select("h1").text();
        String content = FormatContentUtil.formatContent(doc.select(".content").html()).replaceAll("<div>", "").replaceAll("</div>", "");
        article.setTitle(title);
        article.setContent(content);
        return article;
    }

    public Map<String, String> extractPictureListFromContent(String content) {
        String fetUrl = "http://admin.cms.qiaoliba.com/pictureFetch.do?status=1";
        Map resultMap = new HashMap();
        String sourceUrl;
        JSONObject array;
        if (!StringUtil.isEmpty(content)) {
            Document doc = Jsoup.parse(content);
            Elements imgs = doc.getElementsByTag("img");
            int i = 0;
            String urls = "";
            for (Element img : imgs) {
                sourceUrl = "http://www.60mil.com" + img.attr("src").trim();
                urls = urls + ",21cn.com," + sourceUrl;
            }
            if (!StringUtil.isEmpty(urls)) {
                urls = urls.substring(",21cn.com,".length(), urls.length());
            }
            String json = HttpCaller.post(fetUrl).addParam("url", urls).call().getString();
            System.out.println("json:"+json);
            array = JSONObject.parseObject(json);
            for (Element img : imgs) {
                sourceUrl = img.attr("src").trim();
                JSONObject obj = array.getJSONObject("http://www.60mil.com" + sourceUrl);
                String url = obj.getString("url");

                resultMap.put(sourceUrl, url);
            }
        }
        return resultMap;
    }

    public static void saveArticle(String line) {
        Article article = new Fetch60mil().getArticle(line);
        String content = article.getContent();
        Map<String, String> urlmap = new Fetch60mil().extractPictureListFromContent(content);
        for (String url : urlmap.keySet()) {
            content = content.replace(url, (CharSequence) urlmap.get(url));
        }

        System.out.println(content);
        HttpCaller.post(postUrl).addParam("title", StringUtil.utf82iso(article.getTitle()))
                .addParam("content", StringUtil.utf82iso(content)).addParam("sourceName", StringUtil.utf82iso("腾云网"))
                .addParam("mainColumnId", "24")
                .addParam("publishStatus", "2")
                .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .addHeader("Accept-Encoding", "gzip, deflate")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Cookie", "admin_auth_key=xxt:value_admin")
                .call();
        System.out.println("finish url:" + line);
    }

    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/article.txt"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        try {
            while ((line = br.readLine()) != null)
                saveArticle(line);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}